import 'package:blocks_blkk/app_configure.dart';
import 'package:blocks_blkk/page/app_entry.dart';
import 'package:blocks_blkk/page/app_intl.dart';
// import 'package:blocks_blkk/page/entity/local_constants.dart';
import 'package:logger/logger.dart';

import 'page/app_home.dart';
import 'page/startup_video.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wakelock/wakelock.dart';

void main() async {
  await ScreenUtil.ensureScreenSize();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Future<AppConfig> loadFulture = AppConfig.loadFromFile();
  var logger = Logger();

  @override
  Widget build(BuildContext context) {
    // 屏幕常亮
    Wakelock.enable();
    // 设置屏幕大小
    ScreenUtil.init(context, designSize: const Size(1334, 750));

    return MaterialApp(
      debugShowCheckedModeBanner: false,
      // You can use the library anywhere in the app even in theme
      theme: ThemeData(
        primarySwatch: Colors.blue,
        textTheme: TextTheme(bodyMedium: TextStyle(fontSize: 30.sp)),
      ),
      home: StartupVideo(
        nextWidget: FutureBuilder<AppConfig>(
          future: loadFulture,
          builder: loadBuilder,
        ),
      ),
    );
  }

  Widget loadBuilder(BuildContext context, AsyncSnapshot<AppConfig> snapshot) {
    if (snapshot.hasData) {
      AppConfig appConfig = snapshot.data as AppConfig;
      AppIntl.init(config: appConfig);
      ScreenUtil.init(context, designSize: const Size(1334, 750));
      return const AppEntry();
    } else {
      // logger.d(' 加载 snapshot 空');
      return Container();
    }
  }

  //FlutterBlue flutterBlue;
  @override
  void initState() {
    //初始化APP
    //appInitialize();

    //设置状态栏透明
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
    ));

    //隐藏状态栏
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
    //设置屏幕方向
    SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]);
    super.initState();
  }

  @override
  void dispose() {
    // WakelockPlus.disable();
    Wakelock.disable();
    super.dispose();
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<StatefulWidget> createState() => AppHomePageState();
}
